import { createRouter, createWebHistory } from 'vue-router'
import publicLogin from '@/views/LoginView.vue'
import Home from '@/views/home/HomeView.vue'
import gridLogin from '@/views/GLoginView.vue'
import ScreenView from '@/views/home/ScreenView.vue'
import RegisterView from '@/views/RegisterView.vue'
import PublicHistory from '@/components/public/PublicHistory.vue'
import PublicNew from '@/components/public/PublicNew.vue'
import PublicMain from '@/components/public/PublicMain.vue'

import FeedbackHis from '@/components/gridor/FeedbackHis.vue'
import FeedbackList from '@/components/gridor/FeedbackList.vue'
import FeedbackMain from '@/components/gridor/FeedbackMain.vue'
import FeedbackTodo from '@/components/gridor/FeedbackTodo.vue'
import HomeView from '@/views/nepm/HomeView.vue'
import Login from '@/components/nepm/home/Login.vue'
import HomeMain from '@/components/nepm/home/HomeMain.vue'
import RoleView from '@/views/nepm/RoleView.vue'
import GridorView from '@/views/nepm/GridorView.vue'
import FeedbackView from '@/views/nepm/FeedbackView.vue'
import FeedbackDetail from '@/components/nepm/feedback/FeedbackDetail.vue'
import FeedbackPublic from '@/components/nepm/feedback/FeedbackPublic.vue'
import FeedbackUndo from '@/components/nepm/feedback/FeedbackUndo.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: { title: '首页' }
    },
    {
      path: '/slogin',
      name: 'slogin',
      component: publicLogin,
      meta: { title: '公众监督员-登录' }
    },
    {
      path: '/glogin',
      name: 'glogin',
      component: gridLogin,
      meta: { title: '网格员-登录' }
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
      meta: { title: '注册' }
    },
    {
      path: '/screen',
      name: 'screen',
      component: ScreenView,
      meta: { title: '决策大屏' }
    },
    {
      path: '/public',
      name: 'public',
      component: PublicMain,
      meta: { title: '公众监督员-主页' }
    },
    {
      path: '/padd',
      name: 'add',
      component: PublicNew,
      meta: { title: '公众监督员-添加' }
    },
    {
      path: '/phistory',
      name: 'history',
      component: PublicHistory,
      meta: { title: '公众监督员-历史' }
    },
    {
      path: '/feedback',
      name: 'feedback',
      component: FeedbackMain,
      meta: { title: '网格员-主页' }
    },
    {
      path: '/flist',
      name: 'flist',
      component: FeedbackList,
      meta: { title: '网格员-清单' }
    },
    {
      path: '/fhis',
      name: 'fhis',
      component: FeedbackHis,
      meta: { title: '网格员-历史' }
    },
    {
      path: '/ftodo',
      name: 'ftodo',
      component: FeedbackTodo,
      meta: { title: '网格员-确认' }
    },
    {
      path: '/mlogin',
      name: 'mlogin',
      component: Login,
      meta: { title: '管理员-登录' }
    },
    {
      path: '/mhome',
      name: 'mhome',
      component: HomeView,
      redirect: '/users',
      children: [
        {
          path: '/users',
          name: 'users',
          component: HomeMain,
          meta: { title: '管理页' }
        },
        {
          path: '/roles',
          name: 'roles',
          component: RoleView,
          meta: { title: '管理页' }
        },
        {
          path: '/gridor',
          name: 'gridor',
          component: GridorView,
          meta: { title: '管理页' }
        },
        {
          path: '/mfeedbacks',
          name: 'feedbacks',
          component: FeedbackView,
          meta: { title: '管理页' }
        },
        {
          path: '/detail',
          name: 'detail',
          component: FeedbackDetail,
          meta: { title: '管理页' }
        },
        {
          path: '/mpublic',
          name: 'mpublic',
          component: FeedbackPublic,
          meta: { title: '管理页' }
        },
        {
          path: '/undo',
          name: 'undo',
          component: FeedbackUndo,
          meta: { title: '管理页' }
        }
      ]
    }
  ]
})

const whiteList = ['/login', '/', '/slogin', '/glogin', '/mlogin', '/screen']
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (!whiteList.includes(to.path)) {
    if (!localStorage.getItem('token')) {
      router.push('/')
      return
    }
    next()
    return
  }
  next()
})

export default router
